"""
Case Type   : 服务端工具
Case Name   : 修改配置文件的参数，指定主机名称-N不指定数据库实例路径-D/实例名称-I
Create At   : 2024/09/26
Owner       : @chen
Description :
    1.查看authentication_timeout参数:
    2.设置authentication_timeout参数为59s:
Expect      :
    1.查看成功
    2.设置失败
History     : 
"""

import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

LOG = Logger()


class Tools(unittest.TestCase):
    def setUp(self):
        LOG.info('-----Opengauss_Function_Tools_gs_guc_Case0010开始执行-----')
        self.dbuser_node = Node('dbuser')
        self.constant = Constant()
        self.commonsh = CommonSH()

    def test_server_tools1(self):
        LOG.info('------查看authentication_timeout参数------')
        excute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"cat {macro.DB_INSTANCE_PATH}/postgresql.conf|grep " \
            f"authentication_timeout"
        LOG.info(excute_cmd)
        msg1 = self.dbuser_node.sh(excute_cmd).result()
        LOG.info(msg1)
        self.assertIn('1min', msg1)
        LOG.info('----设置authentication_timeout参数为59s-----')
        excute_cmd2 = f'''source {macro.DB_ENV_PATH};
            gs_guc set -N all -c "authentication_timeout= 59s" '''
        LOG.info(excute_cmd2)
        msg1 = self.dbuser_node.sh(excute_cmd2).result()
        LOG.info(msg1)
        self.assertIn(self.constant.GSGUC_SUCCESS_MSG, msg1, 'guc参数设置失败')
        self.commonsh.restart_db_cluster()
        status = self.commonsh.get_db_cluster_status()
        self.assertTrue('Normal' in status or 'Degraded' in status)

    def tearDown(self):
        LOG.info('----------------无需清理环境-----------------------')
        excute_cmd2 = f'''source {macro.DB_ENV_PATH};
            gs_guc set -N all -c "authentication_timeout= 1min"'''
        LOG.info(excute_cmd2)
        msg1 = self.dbuser_node.sh(excute_cmd2).result()
        LOG.info(msg1)
        self.commonsh.restart_db_cluster()
        status = self.commonsh.get_db_cluster_status()
        self.assertIn(self.constant.GSGUC_SUCCESS_MSG, msg1, 'guc参数设置失败')
        self.assertTrue('Normal' in status or 'Degraded' in status)
        LOG.info('-----Opengauss_Function_Tools_gs_guc_Case0010执行结束-----')
